<?php
//http请求类
class Input {
    static private $instance = null;       //用于存放实例化的对象
    //公共静态方法获取实例化的对象
    static public function getInstance() {
        if(!(self::$instance instanceof self)){
            self::$instance = new self();
        }
        return self::$instance;
    }
    //私有克隆
    private function __clone() {
    }
    private function __construct() {
    }

    /**
     * Fetch an item from the GET array
     *
     * @access public
     * @param string
     * @return string
     * @todo 解决无参数时过滤GET array中不需要的数据
     */
    public function get($index = NULL){
        if($index === NULL AND ! empty($_GET)){
            $get = array();
            // loop through the full _GET array
            foreach (array_keys($_GET) as $key){
                $get[$key] = $this->fetchFromArray($_GET,$key);
            }
            return $get;
        }
        return $this->fetchFromArray($_GET,$index);
    }
    /**
     * Fetch an item from the _POST array
     *
     * @access public
     * @param string
     * @return string
     */
    public function post($index = NULL){
        if($index === NULL AND ! empty($_POST)){
            $post = array();
            // loop through the full _POST array
            foreach (array_keys($_POST) as $key){
                $post[$key] = $this->fetchFromArray($_POST,$key);
            }
            return $post;
        }
        return $this->fetchFromArray($_POST,$index);
    }
    /**
     * Fetch an item from the COOKIE array
     *
     * @access public
     * @param string
     * @return string
     */
    public function cookie($index = ''){
        return $this->fetchFromArray($_COOKIE, $index);
    }

    //处理新增的请求
    // public function add($fields){
    //     $addData = $this->post();
    //     $addData = $this->selectAvailableData($addData,$fields);
    //     return $addData;
    // }
    // //处理更新数据的请求
    // public function update($fields){
    //    $updateData = $this->post(); 
    //    $updateData = $this->selectAvailableData($updateData,$fields);
    //    return $updateData;
    // }
    // //处理更新数据的请求2
    // public function ajaxUpdate($fields){
    //     $updateData = $this->selectAvailableData($this->post(),$fields);
    //     return $updateData;
    // }
    // public function isOne($fields) {
    //     $data = $this->get();
    //     $where = $this->selectAvailableData($data,$fields);
    //     return $where;
    // }
    //对比数据库字段，筛选数据
    public function filter($requestData,$fields) {
        $availableData = array();
        foreach($requestData as $key => $value){
            if(in_array(strtolower($key), $fields)){
                $availableData[$key] = $value;
            }
        }
        return $availableData;
    }

    /**
     * fetch from array
     * @access private
     * @param array
     * @param string
     * @return string
     */
    private function fetchFromArray(&$array,$index = ''){
        if ( ! isset($array[$index])){
            return FALSE;
        }   
        return Tool::setFormString($array[$index]);
    }

}